export default QrcodeCard

function QrcodeCard(data) {
  if (!data.canvasId) return
  var [windowHeight, windowWidth] = this.systemWidthHeight()

  this.data = {
    windowHeight,
    windowWidth,
    canvasId: data.canvasId,
    bg: data.bg, // 背景,
    head: data.head, // 头像
    qrcode: data.qrcode, // 二维码
    updateCanvasSize: data.updateCanvasSize || ''
  }

  // 创建上下文
  var ctx = wx.createCanvasContext(data.canvasId, this)
  this.ctx = ctx
}

QrcodeCard.prototype.init = function () {
  var canvasSize = this.getRealityCanvasScale()
  if (this.data.updateCanvasSize) this.data.updateCanvasSize(canvasSize)

  this.drawBg()
  this.drawQrcode()
  this.drawHead()

  return this
}

// 计算canvas的宽高
QrcodeCard.prototype.getRealityCanvasScale = function () {
  var canvasWidth = this.uiToReality(650) + 'px'
  var canvasHeight = this.uiToReality(667) + 'px'
  return { canvasWidth, canvasHeight }
}

QrcodeCard.prototype.systemWidthHeight = function () {
  var systemInfo = wx.getSystemInfoSync()
  var windowWidth = systemInfo.windowWidth
  var windowHeight = systemInfo.windowHeight

  return [windowHeight, windowWidth]
}

QrcodeCard.prototype.uiToReality = function (uiSize, windowWidth = this.data.windowWidth) {
  return uiSize / 750 * windowWidth
}

// 绘制背景
QrcodeCard.prototype.drawBg = function () {
  var canvasWidth = this.uiToReality(650)
  var canvasHeight = this.uiToReality(667)
  var startY = this.uiToReality(40)
  
  this.ctx.drawImage(this.data.bg, 0, startY, canvasWidth, canvasHeight - startY)
  this.ctx.draw(true)
}

// 绘制二维码
QrcodeCard.prototype.drawQrcode = function () {
  var w = this.uiToReality(350)
  var h = this.uiToReality(350)
  var x = this.uiToReality(150)
  var y = this.uiToReality(186)
  var startY = this.uiToReality(30)

  if (!this.data.qrcode) return
  this.ctx.drawImage(this.data.qrcode, x, y + startY, w, h)
  this.ctx.draw(true, function () {
    console.log('二维码绘制完成')
  })
}

// 绘制头像
QrcodeCard.prototype.drawHead = function () {
  var w = this.uiToReality(110)
  var h = this.uiToReality(110)
  var x = this.uiToReality(263)
  var y = this.uiToReality(0)
  var startY = this.uiToReality(40)

  this.ctx.arc(x + w / 2 + 6, h - startY, w / 2, 0, 2 * Math.PI)
  this.ctx.fill()
  this.ctx.clip()

  if (!this.data.head) return
  this.ctx.drawImage(this.data.head, x + 6, 8, w, h)
  this.ctx.draw(true, function () {
    console.log('头像绘制')
  })
}

// 保存为图片
QrcodeCard.prototype.saveCard = function (callback) {
  var _this = this
  wx.canvasToTempFilePath({
    x: 0,
    y: 0,
    canvasId: _this.data.canvasId,
    success: function (res) {
      console.log('res - saveCard', res.tempFilePath, 'ssssssss', _this.data.canvasId, callback)
      if (callback) {
        callback(res.tempFilePath);
      } else {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(res) {
            wx.showToast({
              title: '保存成功',
              duration: 1000
            })
          }
        })
      }
    }
  })
}